package com.qq.weixin.mp.api.base.impl;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.hxexp.weixin.common.exception.APIException;
import com.hxexp.weixin.common.httpclient.Json2ObjectHandler;
import com.qq.weixin.mp.api.base.BaseAPI;
import com.qq.weixin.mp.api.base.model.AccessTokenBean;
import com.qq.weixin.mp.api.base.model.CallbackIPBean;

/**
 * 微信公众平台基础接口实现类
 * @author li.jiang
 *
 */
@Component
public final class BaseAPIImpl implements BaseAPI {
    private final CloseableHttpClient httpclient = HttpClients.createDefault();
    @Value("${base_api.access_token.url}")
    private String accessTokenApiUrl;
    @Value("${base_api.callbackip.url}")
    private String callbackIPApiUrl;

    /* (non-Javadoc)
     * @see com.hxexp.weixin.api.base.BaseAPI#getAccessToken()
     */
    @Override
    public AccessTokenBean getAccessToken(String appId, String appSecret) throws APIException {
        String url = String.format("%s?grant_type=client_credential&appid=%s&secret=%s", accessTokenApiUrl, appId, appSecret);
        HttpGet httpget = new HttpGet(url);
        // 指定对象映射器的属性命名策略为小写加下划线
        PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy s = new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy();
        TypeReference<AccessTokenBean> typeReference = new TypeReference<AccessTokenBean>() {
        };
        try {
            return httpclient.execute(httpget, new Json2ObjectHandler<AccessTokenBean>(typeReference, s));
        } catch (ClientProtocolException e) {
            throw new APIException(e);
        } catch (IOException e) {
            throw new APIException(e);
        }
    }

    /* (non-Javadoc)
     * @see com.hxexp.weixin.api.base.BaseAPI#getCallbackIP(java.lang.String)
     */
    @Override
    public CallbackIPBean getCallbackIP(String accessToken) throws APIException {
        String url = String.format("%s?access_token=%s", callbackIPApiUrl, accessToken);
        HttpGet httpget = new HttpGet(url);
        // 指定对象映射器的属性命名策略为小写加下划线
        PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy s = new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy();
        TypeReference<CallbackIPBean> typeReference = new TypeReference<CallbackIPBean>() {
        };
        try {
            return httpclient.execute(httpget, new Json2ObjectHandler<CallbackIPBean>(typeReference, s));
        } catch (ClientProtocolException e) {
            throw new APIException(e);
        } catch (IOException e) {
            throw new APIException(e);
        }
    }

}
